﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public class TreeNodeModel : INotifyPropertyChanged
    {
        /// <summary>
        /// 选中状态
        /// </summary>
        private bool? _isChecked;

        /// <summary>
        /// 折叠状态
        /// </summary>
        private bool _isExpanded;

        /// <summary>
        /// 构造
        /// </summary>
        public TreeNodeModel()
        {
            Children = new ObservableCollection<TreeNodeModel>();
            _isChecked = false;
            IsExpanded = false;
            Icon = "/Images/16_16/folder_go.png";
        }

        /// <summary>
        /// 构造
        /// </summary>
        public TreeNodeModel(TreeNodeModel parent):this()
        {
            Parent = parent;
            Parent.Children.Add(this);
            if (Parent?.IsChecked ==true)
            {
                IsChecked = true;
            }
        }

        /// <summary>
        /// 键值
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// code值
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 显示的字符
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 指针悬停时的显示说明
        /// </summary>
        public string ToolTip => $"{Id}-{Name}";

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool? IsChecked
        {
            get => _isChecked;
            set
            {
                if (value != _isChecked)
                {
                    _isChecked = value;
                    NotifyPropertyChanged(nameof(IsChecked));
                }
            }
        }

        public bool IsLeaf => !Children.Any();

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get => _isExpanded;
            set
            {
                if (value != _isExpanded)
                {
                    //折叠状态改变
                    _isExpanded = value;
                    NotifyPropertyChanged(nameof(IsExpanded));
                }
            }
        }

        /// <summary>
        /// 父项
        /// </summary>
        public TreeNodeModel Parent { get; set; }

        /// <summary>
        /// 子项
        /// </summary>
        public ObservableCollection<TreeNodeModel> Children { get; set; }

        /// <summary>
        /// 设置所有子项的选中状态
        /// </summary>
        public void SetChildrenChecked()
        {
            if (Children != null && Children.Count > 0 && IsChecked != null)
            {
                foreach (TreeNodeModel child in Children)
                {
                    child.IsChecked = IsChecked;
                    if (child.Children != null && Children.Count > 0)
                    {
                        child.SetChildrenChecked();
                    }
                }
            }
        }

        public void SetChildrenChecked(bool isCheck)
        {
            if (Children != null && Children.Count > 0)
            {
                foreach (TreeNodeModel child in Children)
                {
                    child.IsChecked = isCheck;
                    if (child.Children != null && Children.Count > 0)
                    {
                        child.SetChildrenChecked(isCheck);
                    }
                }
            }
        }

        /// <summary>
        /// 设置所有子项展开状态
        /// </summary>
        /// <param name="isExpanded"></param>
        public void SetChildrenExpanded(bool isExpanded)
        {
            foreach (TreeNodeModel child in Children)
            {
                child.IsExpanded = isExpanded;
                child.SetChildrenExpanded(isExpanded);
            }
        }

        /// <summary>
        /// 属性改变事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }

        /// <summary>
        /// 附加属性
        /// </summary>
        public object Tag { get; set; }

        public void SetParentChecked()
        {
            if (Parent != null)
            {
                if (Parent.Children.Any(c => c.IsChecked == null))
                {
                    Parent.IsChecked = null;
                }
                else if (Parent.Children.All(c => c.IsChecked != true))
                {
                    Parent.IsChecked = false;
                }
                else if (Parent.Children.Count(c => c.IsChecked == true) == Parent.Children.Count)
                {
                    Parent.IsChecked = true;
                }
                else if (Parent.Children.Count(c => c.IsChecked == true) < Parent.Children.Count)
                {
                    Parent.IsChecked = null;
                }

                if (Parent.Parent != null)
                {
                    Parent.SetParentChecked();
                }
            }
        }

        /// <summary>
        /// 获取所有子项的选中状态
        /// </summary>
        /// <return>true表示有选中的，false表示没有选中的</return>
        public bool getChildrenChecked()
        {
            bool value = false;
            foreach (TreeNodeModel child in Children)
            {
                if (child.IsChecked != null && child.IsChecked.Value)
                {
                    value = true;
                    break;
                }
            }

            return value;
        }
    }
}
